package com.travel.Controller;

import com.github.pagehelper.PageInfo;
import com.travel.Bean.Manager;
import com.travel.Service.Impl.ManagerServiceImpl;
import com.travel.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Api("管理员主页")
@RestController
@RequestMapping("SuperPage")
public class SuperIndexController {


    @Autowired
    private ManagerServiceImpl managerService;


    //获取管理员权限信息
    public String getAdminRole() {
        String role;
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            role = ((UserDetails) principal).getAuthorities().toString();
        } else {
            role = principal.toString();
        }
        return role;
    }



    @ApiOperation(value = "管理员权限获取")
    @PostMapping("adminRole")
    public String adminRole(){

//        HttpSession session = request.getSession();


        String role = getAdminRole();
        if(role.indexOf("ROLE_super")>0){
//            session.setAttribute("role","super.json");
            return "super.json";
        }


//        session.setAttribute("role","init.json");
        return "init.json";
    }

    @ApiOperation("超级管理员首页")
    @GetMapping("sIndex")
    public ModelAndView sIndex(){
        ModelAndView modelAndView = new ModelAndView("index");
        modelAndView.addObject("role",adminRole());
        return modelAndView;
    }

    @ApiOperation("管理员列表页")
    @RequestMapping("SuperAdminPage")
    public ModelAndView SuperAdminPage(){
        return new ModelAndView("super/SuperIndex");
    }

    /**
     * 超级指数
     *
     * @param adminName   管理员名称
     * @param cultureName 文化名字
     * @param page        页面
     * @param limit       记录条数
     * @return {@link R}
     */
    @ApiOperation("管理员列表")
    @PostMapping("SuperAdminList")
//    @GetMapping("SuperIndex")
    public R SuperIndex(String adminName, String cultureName, @RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "15") Integer limit){

        PageInfo<Manager> managerPageInfo = managerService.findManagerList(page,limit,adminName,cultureName);

        return R.ok("0",managerPageInfo.getList(),managerPageInfo.getTotal());
    }


    /**
     * 删除管理
     *
     * @param id id
     * @return {@link R}
     */
    @ApiOperation("删除管理员")
    @PostMapping("DelAdmin")
    public R DelAdmin(Integer id){
        if (managerService.deleteManagerById(id)>0)
            return R.ok(0);
        return R.no(1);
    }
}
